import { Injectable } from '@angular/core';
import {Observable, of} from 'rxjs';

import { Hero } from './hero';
import { HEROES } from './mock-heroes';
import {MessageService } from './message.service';

@Injectable({
  providedIn: 'root'
})
export class HeroService {

  constructor(private messageService: MessageService) { }

  getHeroes(): Observable<Hero[]> {
    this.messageService.add('HeroService: fetched heroes');
    return of(HEROES);
  }

  getHero(rank: number): Observable<Hero> {
    this.messageService.add(`HeroService: fetched hero (rank=${rank})`);
    return of(HEROES.find(hero => {
      return hero.rank === rank;
    }));
  }
}
